<?php
/**
 * Created by PhpStorm.
 * User: Yueool
 * Date: 2018/8/10
 * Time: 12:30
 */

namespace common\models;

use Yii;


class Tag{

    /**
     * 得到SQL
     * @param $id
     * @return bool
     */
    public static function getSqlCondition($tags_str, $as = ''){
        $as = (empty($as)) ? '' : $as.".";
        $tags = explode(",", $tags_str);
        $condition = "";
        if(empty($tags_str)){
            return "";
        }else if(count($tags) == 1){
            return " AND ".$as.$tags[0]."=1";
        }else if(count($tags) > 1){
            foreach($tags as $tag){
                $condition .= " OR ".$as.$tag."=1";
            }
            $condition = preg_replace('/^ OR /', '', $condition);
            return " AND (".$condition.")";
        }
    }

}